//
//  YSGNurseAppointmentDetailsController.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/3/3.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit

class YSGNurseAppointmentDetailsController: BaseViewController {

    var model: YSGNurseAppointmentModel?
    
    var htmlModel: AgreementViewModel?
    
    override func viewDidLoad() {
            super.viewDidLoad()
            self.title = "护理预约"
            view.addSubview(tableView)
            tableView.snp.makeConstraints { make in
                make.edges.equalTo(view.safeAreaLayoutGuide).priority(.low)
                make.bottom.equalToSuperview()
            }
        tableView.isHidden = true
        
        loadData {[weak self] _model, errorMsg, code in
            switch code {
            case .success:
                self?.htmlModel = _model
                self?.tableView.reloadData()
                self?.tableView.isHidden = false
            case .failure:
                HUDTool.showError(message: errorMsg)
            }
        };
    }
    
    func loadData(finished: @escaping (_ model: AgreementViewModel?,_ errorMsg: String?, _ code: HttpCode) -> Void)  {
        AgreementViewModel.inquiryAgreementString(type: model?.chargeItemCode ?? "") { (model, errorMsg, code) in
            switch code {
            case .failure:
                finished(nil,errorMsg,.failure)
            case .success:
                finished(model, "" ,.success)
            }
        }
    }
    
    private lazy var bottomView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 150))
        view.backgroundColor = UIColor.clear
        let appointmentButton = UIButton()
        view.addSubview(appointmentButton)
        appointmentButton.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(32)
            make.right.equalToSuperview().offset(-32)
            make.top.equalToSuperview().offset(29)
            make.height.equalTo(49)
        }
        appointmentButton.setTitle("立 即 预 约", for: .normal)
        appointmentButton.titleLabel?.font = UIFont(name: "PingFangSC-Semibold", size: 16)
        appointmentButton.setTitleColor(UIColor.white, for: .normal)
        appointmentButton.backgroundColor = UIColor.mainColor
        appointmentButton.layer.cornerRadius = 24
        appointmentButton.layer.masksToBounds = true
        appointmentButton.addTarget(self, action: #selector(appointmentButtonClick), for: .touchUpInside)

        return view
    }()
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        tableView.backgroundColor = .background
        tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 1))
        tableView.tableFooterView = UIView()
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = bottomView

        tableView.register(YSGNurseAppointmentCell.self, forCellReuseIdentifier: YSGNurseAppointmentCell.reuseIdentifier())
        tableView.register(YSGNurseAppointmentProcessCell.nibForCell(), forCellReuseIdentifier: YSGNurseAppointmentProcessCell.reuseIdentifier())
        tableView.register(YSGNurseAppointmentMarkCell.nibForCell(), forCellReuseIdentifier: YSGNurseAppointmentMarkCell.reuseIdentifier())
        
        return tableView
    }()
    
    
    @objc private func appointmentButtonClick(){
        let vc = YSGNurseAppointmentMapSearchController()
        vc.model = model!
        navigationController?.pushViewController(vc, animated: true)
    }
    
    
 
}

    extension YSGNurseAppointmentDetailsController: UITableViewDataSource {
        func numberOfSections(in tableView: UITableView) -> Int {
            return 3
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            
            switch indexPath.section{
            case 0:
                let cell = tableView.dequeueReusableCell(withIdentifier: YSGNurseAppointmentCell.reuseIdentifier(),
                                                         for: indexPath) as! YSGNurseAppointmentCell
                cell.config(model: model!)
                
                return cell
            case 1:
                let cell = tableView.dequeueReusableCell(withIdentifier: YSGNurseAppointmentProcessCell.reuseIdentifier(),
                                                         for: indexPath) as! YSGNurseAppointmentProcessCell

                
                return cell
            default:
                let cell = tableView.dequeueReusableCell(withIdentifier: YSGNurseAppointmentMarkCell.reuseIdentifier(),
                                                         for: indexPath) as! YSGNurseAppointmentMarkCell
                cell.htmlModel = htmlModel
                cell.configuration()
                return cell
            }
            
            
        }
    }

    extension YSGNurseAppointmentDetailsController: UITableViewDelegate {
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            return UIView(frame: CGRect(x: 0, y: 0, width: screenW, height: 10))
        }
        
        func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
            return nil
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            switch indexPath.section{
            case 0:
                return 60
            case 1:
                return 120
            default:
                return 400
            }
        }
        
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 10
        }
        
        func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
            return 0
        }
    }

